package models

import (
	"ShopSupplierApi/models/mysql"
	"ShopSupplierApi/utils"
	dbsql "database/sql"
	"strconv"
)

//MemberType 会员类型
type MemberType int

const (
	//MemberType_All 所有
	MemberType_All MemberType = iota
	//MemberType_Order 下单
	MemberType_Order
	//MemberType_Pay 成交
	MemberType_Pay
	//MemberType_Back 退款
	MemberType_Back
)

//String MemberType说明
func (m MemberType) String() string {
	switch m {
	case MemberType_Order:
		return "所有"
	case MemberType_Pay:
		return "成交"
	case MemberType_Back:
		return "退款"
	default:
		return "所有"
	}
}

//memberResult 会员信息后期处理 附加会员帐号信息
func memberResult(member map[string]interface{}) {
	if member == nil {
		return
	}
	if memberID, ok := member["member_id"]; ok {
		sql := " SELECT login_account,login_type FROM sdb_pam_members WHERE member_id = ? "
		mysql.RowsCallbackResult(sql, func(rows *dbsql.Rows) {
			var loginAccount string
			var loginType string
			for rows.Next() {
				err := rows.Scan(&loginAccount, &loginType)
				if err == nil {
					if _, ok = member[loginType]; !ok {
						member[loginType] = loginAccount
					}
				}
			}
		}, memberID)
	}
}

//memberListResult 会员列表后期处理
func memberListResult(memberList []map[string]interface{}) {
	if memberList == nil || len(memberList) < 1 {
		return
	}
	sql := " SELECT login_account,login_type,member_id FROM sdb_pam_members WHERE member_id IN (0"
	for _, v := range memberList {
		sql += "," + strconv.FormatInt(v["member_id"].(int64), 10)
	}
	sql += " ) ORDER BY member_id desc "
	mysql.RowsCallbackResult(sql, func(rows *dbsql.Rows) {
		mres := make(map[uint64]map[string]interface{}, 0)
		var mid uint64
		var loginAccount string
		var loginType string
		for rows.Next() {
			err := rows.Scan(&loginAccount, &loginType, &mid)
			if err == nil {
				if _, ok := mres[mid]; !ok {
					mres[mid] = map[string]interface{}{loginType: loginAccount}
				}
				mres[mid][loginType] = loginAccount
			}
		}
		for _, v := range memberList {
			if m, ok := mres[uint64(v["member_id"].(int64))]; ok {
				for k, vv := range m {
					v[k] = vv
				}
			}
		}
	})
}

//ProductMemberList 货品购买者会员列表
//param supplierID uint64 供应商ID
//param productID uint64 货品ID
//param p *Page 数据分页信息
func ProductMemberList(supplierID uint64, productID uint64, p *Page) []map[string]interface{} {
	var result []map[string]interface{}
	sql := "SELECT c.member_id,c.login_account,c.login_type,e.`name` lv_name,d.order_num,d.regtime,d.point,f.ordersnum,f.amount" +
		" FROM sdb_b2c_order_items a LEFT JOIN lcf_b2c_orders b ON a.order_id = b.order_id " +
		" LEFT JOIN sdb_pam_members c ON b.member_id = c.member_id " +
		" LEFT JOIN sdb_b2c_members d ON c.member_id = d.member_id " +
		" LEFT JOIN sdb_b2c_member_lv e ON d.member_lv_id = e.member_lv_id " +
		" LEFT JOIN lcf_view_member_orders f ON f.member_id = d.member_id  WHERE  b.pay_status = \"1\" " +
		" AND a.product_id = ?  AND b.supplier_id = ?  GROUP BY c.member_id"
	result, err := mysql.Rows(p.SQL(sql), productID, supplierID)
	if err == nil {
		p.Rows = int64(len(result))
		p.CountRows(sql, productID, supplierID)
		memberListResult(result)
	}
	return result
}

//SimpleMemberInfo 基本会员信息
//param memberID uint64 会员ID
func SimpleMemberInfo(memberID uint64) map[string]interface{} {
	result := make(map[string]interface{}, 0)
	sql := "select a.member_id,a.order_num,SUM(c.ordersnum) ordersnum,b.`name`,a.point from sdb_b2c_members a LEFT JOIN sdb_b2c_member_lv b ON a.member_lv_id = b.member_lv_id " +
		" LEFT JOIN lcf_view_member_orders c ON a.member_id = c.member_id WHERE a.member_id = ? GROUP BY a.member_id "
	row, err := mysql.Row(sql, memberID)
	if err == nil {
		var mid uint64
		var orderNum uint32
		var orderSum uint32
		var name string
		var point int
		err = row.Scan(&mid, &orderNum, &orderSum, &name, &point)
		if err == nil {
			result["member_id"] = mid
			result["order_num"] = orderNum
			result["ordersnum"] = orderSum
			result["name"] = name
			result["point"] = point
		}
		memberResult(result)
	}
	return result
}

//MemberList 指定类型会员列表
//param supplierID uint64 供应商ID
//param mType MemberType 会员类型
//param search string   搜索关键字
//param p   *Page   数据分页信息
func MemberList(supplierID uint64, mType MemberType, search string, p *Page) []map[string]interface{} {
	sql := "SELECT a.login_account,a.login_type,b.regtime,b.order_num,c.name lv_name,d.ordersnum,d.amount,a.member_id,b.point " +
		" FROM sdb_pam_members a LEFT JOIN sdb_b2c_members b ON a.member_id = b.member_id " +
		" LEFT JOIN sdb_b2c_member_lv c ON c.member_lv_id = b.member_lv_id " +
		" LEFT JOIN lcf_view_member_orders d ON a.member_id = d.member_id "
	switch mType {
	case MemberType_Order:
		sql += "  LEFT JOIN lcf_b2c_orders e ON b.member_id = e.member_id WHERE e.supplier_id = @suplier_id AND b.order_num > 0"
		break
	case MemberType_Pay:
		sql += " LEFT JOIN lcf_b2c_orders e ON b.member_id = e.member_id WHERE e.supplier_id = @supplier_id AND d.ordersnum > 0 "
		break
	case MemberType_Back:
		sql += " LEFT JOIN lcf_b2c_orders e ON e.member_id = a.member_id " +
			" WHERE b.order_num > 0 AND (e.pay_status = \"5\" OR e.pay_status = \"4\" ) "
		sql += " AND e.supplier_id = @supplier_id "
		break
	}
	if search != "" {
		search = "%" + search + "%"
		sql += " AND a.login_account like @search"
	}
	sql += " GROUP BY a.member_id ORDER BY a.member_id desc "
	sql, args, err := mysql.ParseSQL(sql, map[string]interface{}{"supplier_id": supplierID, "search": search})
	if err != nil {
		utils.Logger.Warn("SQL语句解析错误", sql, err)
		return []map[string]interface{}{}
	}
	rows, err := mysql.Rows(p.SQL(sql), args...)
	if err != nil {
		utils.Logger.Error(err.Error())
		return []map[string]interface{}{}
	}
	p.Rows = int64(len(rows))
	p.CountRows(sql, args...)
	memberListResult(rows)
	return rows
}
